package entity;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;

import entity.item.Item;

import loading.ImageLoader;
import main.GameList;
import main.Inventory;
import math.Equations;
import math.Vector2D;

public class Player extends DynamicEntity
{
	private boolean up, down, left, right;
	private Inventory inv;
	private static final int SPEED = 150;
	public static final int USE_RANGE = 15;
	private final double PICK_UP_ANGLE = Math.toRadians(60);
	public Player(Point2D pos, GameList game, ImageLoader img, Inventory inv)
	{
		super(pos, game, img);
		up = down = left = right = false;
		this.inv = inv;
	}
	@Override
	//also draw the inventory
	public void draw(Graphics2D g)
	{
		super.draw(g);
		inv.draw(g);
	}
	public void moveUp()
	{
		if (!up)
		{
			getVelocity().add(new Vector2D(SPEED, Math.PI*.5));
			if (getVelocity().getMagnitude() > 0.001)
				getVelocity().setMagnitude(SPEED);
			up = true;
		}
	}
	public void moveDown()
	{
		if (!down)
		{
			getVelocity().add(new Vector2D(SPEED, Math.PI*1.5));
			if (getVelocity().getMagnitude() > 0.001)
				getVelocity().setMagnitude(SPEED);
			down = true;
		}
	}
	public void moveLeft()
	{
		if (!left)
		{
			getVelocity().add(new Vector2D(SPEED, Math.PI));
			if (getVelocity().getMagnitude() > 0.001)
				getVelocity().setMagnitude(SPEED);
			left = true;
		}
	}
	public void moveRight()
	{
		if (!right)
		{
			getVelocity().add(new Vector2D(SPEED, 0));
			if (getVelocity().getMagnitude() > 0.001)
				getVelocity().setMagnitude(SPEED);
			right = true;
		}
	}
	public void stopUp()
	{
		if (up)
		{
			setVelocity(new Vector2D(0, 0));
			up = right = left = down = false;
		}
	}
	public void stopDown()
	{
		if (down)
		{
			setVelocity(new Vector2D(0, 0));
			up = right = left = down = false;
		}
	}
	public void stopLeft()
	{
		if (left)
		{
			setVelocity(new Vector2D(0, 0));
			up = right = left = down = false;
		}
	}
	public void stopRight()
	{
		if (right)
		{
			setVelocity(new Vector2D(0, 0));
			up = right = left = down = false;
		}
	}
	public void stop()
	{
		setVelocity(new Vector2D(0, 0));
		up = right = left = down = false;
	}
	public void pickUp(Item item)
	{
		inv.add(item);
	}
	//determines if the player is facing an entity
	public boolean facing(Entity ent)
	{
		double angleItem = Equations.normalizeAngle(Equations.pointToPointAngle(getPosition(), ent.getPosition()));
		double ang = getAngle();
		return (angleItem < ang + PICK_UP_ANGLE/2 && angleItem > ang - PICK_UP_ANGLE/2);
	}
	//checks if the player has a certain item
	public boolean has(Item item)
	{
		return inv.contains(item);
	}
	//dispose of an item
	public void dispose(Item item)
	{
		inv.remove(item);
	}
}
